<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password - Teaching Management System</title>
    <link rel="stylesheet" href="account-login.css">
</head>
<body>
    <div class="container">
        <h2>reset password</h2>
        <p style="color: #718096; margin-bottom: 30px; font-size: 14px; line-height: 1.5;">
            Please enter the student ID and phone number you used for registration, and we will help you reset your password
        </p>
        <form id="forgot-password-form">
            <div class="input-group">
                <label for="userid">Student ID</label>
                <input type="text" id="userid" placeholder="Please enter your registered student ID" required>
                <span id="us-error-message" class="error-message"></span>
            </div>
            
            <div class="input-group">
                <label for="phone">cell-phone number</label>
                <input type="tel" id="phone" placeholder="Please enter the phone number used for registration" required>
                <span id="ph-error-message" class="error-message"></span>
            </div>
            
            <button type="submit" class="button">
                <span class="button-text">Verify identity</span>
            </button>
            
            <div id="success-message" class="success-message"></div>
        </form>
        
        <div class="auth-links">
            <p>
                Remember the password?<a href="login.html">Return to login</a><br>
                Don't have an account?<a href="register.html">Register Now</a>
            </p>
        </div>
    </div>
    
    <script src="../database.js"></script>
    <script src="account.js"></script>
    <script>
        // When entering the password reset page, clear the current user ID for password reset
        localStorage.removeItem('resetPassword');

        // When leaving the user ID input field, verify if the user ID is filled
        document.getElementById('userid').addEventListener('blur', () => {
            checkUserid();
        });

        // When leaving the phone number input field, verify if the phone number is filled
        document.getElementById('phone').addEventListener('blur', () => {
            checkPhone();
        });

        document.getElementById('forgot-password-form').addEventListener('submit', (e) => {
            e.preventDefault();
            
            const button = e.target.querySelector('button');
            const buttonText = button.querySelector('.button-text');
            const userid = document.getElementById('userid').value;
            const phone = document.getElementById('phone').value;

            // Check if the information is filled
            if (!checkUserid() || !checkPhone()) {
                return;
            }

            // Show loading state
            button.classList.add('loading');
            button.disabled = true;
            buttonText.textContent = 'Verifying...';

            // Simulate network delay
            setTimeout(() => {
                // Check if the user ID exists
                const item = JSON.parse(localStorage.getItem(userid));
                if (!item) {
                    document.getElementById('us-error-message').textContent = 'This student ID does not exist';
                    resetButtonState();
                    return;
                }

                // Check if the phone number is correct
                if (item.phone !== phone) {
                    document.getElementById('ph-error-message').textContent = 'Phone number does not match registration information';
                    resetButtonState();
                    return;
                }

                // Clear error messages
                document.getElementById('us-error-message').textContent = '';
                document.getElementById('ph-error-message').textContent = '';

                // Show success message
                document.getElementById('success-message').textContent = 'Identity verification successful！Redirecting to password reset page...';

                // Store the user ID that needs to change the password in localStorage, then jump to the new password setting page
                localStorage.setItem('resetPassword', userid);
                
                setTimeout(() => {
                    window.location.href = 'set-new-password.html';
                }, 2000);
            }, 1000);
        });

        // Reset button state
        function resetButtonState() {
            const button = document.querySelector('button');
            const buttonText = button.querySelector('.button-text');
            button.classList.remove('loading');
            button.disabled = false;
            buttonText.textContent = 'Verify identity';
        }

        // Add input field animation effect
        document.querySelectorAll('.input-group input').forEach(input => {
            input.addEventListener('focus', function() {
                this.parentElement.classList.add('focused');
            });
            
            input.addEventListener('blur', function() {
                if (!this.value) {
                    this.parentElement.classList.remove('focused');
                }
            });
        });
    </script>
</body>
</html>